<?php
$user_id = session('user_id');
//$user_id = 1;
require_once(EC_PATH . '/languages/' .$_CFG['lang']. '/user.php');
include_once(EC_PATH . '/includes/lib_order.php');
include_once(EC_PATH . '/includes/lib_transaction.php');
include_once(EC_PATH . '/includes/lib_clips.php');
include_once(EC_PATH . '/includes/lib_payment.php');

$page = _POST('page', 1);
$type = _POST('type', 'all');
#全部 all

#待评价  wait_comment

//await_pay 待付款
//await_ship 待发货
//shipped 待收货
//finished 历史订单
//back     退换货
if (!in_array($type, array('all', 'await_pay', 'await_ship', 'shipped', 'finished', 'unconfirmed', 'wait_comment', 'back'))) {
	abort('参数错误!');
}
//$record_count = $db->getOne("SELECT COUNT(order_id) FROM " .$ecs->table('order_info'). " WHERE user_id = '$user_id'". GZ_order_query_sql($type));

$orders = GZ_get_user_orders($user_id, 6, ($page-1)*6, $type);

foreach ($orders as $key => $value) {
	$orders[$key]['order_time'] = formatTime($value['order_time']);
	$goods_list = GZ_order_goods($value['order_id']);
	//$orders[$key]['ss'] = $goods_list;
	$goods_list_t = array();
	// $goods_list = API_DATA("SIMPLEGOODS", $goods_list);
	foreach ($goods_list as $v) {
		$goods_list_t[] = array(
            "rec_id" => $v['rec_id'],
		    "goods_id" => $v['goods_id'],
		    "name" => $v['goods_name'],
		    "goods_number" => $v['goods_number'],
		    "subtotal" => price_format($v['subtotal'], false),
		    "formated_shop_price" => price_format($v['goods_price'], false),
            //'is_commented' => $v['isEvaluated'] == 1,
            "img" => array(
                'small'=>API_DATA('PHOTO', $v['goods_thumb']),
			    'thumb'=>API_DATA('PHOTO', $v['goods_img']),
			    'url' => API_DATA('PHOTO', $v['original_img'])
			)
		);
	}

    if ($order_detail['pay_id'] > 0)
    {
        $payment = payment_info($order_detail['pay_id']);
    }
    $subject = $orders[$key]['goods_list'][0]['name'].'等'.count($orders[$key]['goods_list']).'种商品';
    $orders[$key]['pay_code'] = $payment['pay_code']?:'';
    $orders[$key]['order_amount'] = $order_detail['order_amount'];
    $orders[$key]['subject'] = $subject;
    $orders[$key]['desc'] = $subject;
	$order_detail = get_order_detail($value['order_id'], $user_id);
	$orders[$key]['formated_integral_money']   = $order_detail['formated_integral_money'];//积分 钱
	$orders[$key]['formated_bonus']   = $order_detail['formated_bonus'];//红包 钱
	$orders[$key]['formated_shipping_fee']   = $order_detail['formated_shipping_fee'];//运送费
    $orders[$key]['goods_list'] = $goods_list_t;
}

return $orders;

//function
/**
 *  获取用户指定范围的订单列表
 *
 * @access  public
 * @param   int         $user_id        用户ID号
 * @param   int         $num            列表最大数量
 * @param   int         $start          列表起始位置
 * @return  array       $order_list     订单列表
 */
function GZ_get_user_orders($user_id, $num = 10, $start = 0, $type = 'await_pay')
{
    /* 取得订单列表 */
    $arr    = array();

    $sql = "SELECT order_id, order_sn, order_status, shipping_status, pay_status, add_time, " .
           "(goods_amount + shipping_fee + insure_fee + pay_fee + pack_fee + card_fee + tax - discount) AS total_fee ".
           " FROM " .$GLOBALS['ecs']->table('order_info') .
           " WHERE user_id = '$user_id' " . GZ_order_query_sql($type) . " ORDER BY add_time DESC";
           // print_r($sql);exit;
           // file_put_contents('./a.txt', $type.':'.$sql."\n", FILE_APPEND);
    $res = $GLOBALS['db']->SelectLimit($sql, $num, $start);
   	while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = array(
            'order_id'       => $row['order_id'],
            'order_sn'       => $row['order_sn'],
            'order_time'     => local_date($GLOBALS['_CFG']['time_format'], $row['add_time']),
            'order_status'   => get_order_status($row['order_status'], $row['shipping_status'], $row['pay_status']),
            'total_fee'      => price_format($row['total_fee'], false),
            //'is_commented'	=> $GLOBALS['db']->getOne("SELECT COUNT(rec_id) FROM ".$GLOBALS['ecs']->table('order_goods')." WHERE `order_id`={$row['order_id']} AND `isEvaluated`=0") ? 0 : 1 ,
        );
    }

    return $arr;
}





/**
 * 取得订单商品
 * @param   int     $order_id   订单id
 * @return  array   订单商品数组
 */
function GZ_order_goods($order_id)
{
//    $sql = "SELECT o.*, " .
//            "o.goods_price * o.goods_number AS subtotal,g.goods_thumb,g.original_img,g.goods_img,o.isEvaluated,o.rec_id " .
//            "FROM " . $GLOBALS['ecs']->table('order_goods') . " as o LEFT JOIN ".$GLOBALS['ecs']->table('goods') . " AS g ON o.goods_id = g.goods_id" .
//            " WHERE o.order_id = '$order_id'";
    $sql = "SELECT o.*, " .
        "o.goods_price * o.goods_number AS subtotal,g.goods_thumb,g.original_img,g.goods_img,o.rec_id " .
        "FROM " . $GLOBALS['ecs']->table('order_goods') . " as o LEFT JOIN ".$GLOBALS['ecs']->table('goods') . " AS g ON o.goods_id = g.goods_id" .
        " WHERE o.order_id = '$order_id'";

    $res = $GLOBALS['db']->query($sql);

    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['extension_code'] == 'package_buy')
        {
            $row['package_goods_list'] = get_package_goods($row['goods_id']);
        }
        $goods_list[] = $row;

    }
    //return $GLOBALS['db']->getAll($sql);
    return $goods_list;
}
